package cc.dong.dao.impl;

import cc.dong.dao.BaseDao;
import cc.dong.dao.StatisticsDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoDong
 * @version 1.0.0
 * @ClassName StatisticsDaoImpl.java
 * @Description TODO
 * @createTime 2024年06月03日 11:03:00
 */
public class StatisticsDaoImpl extends BaseDao implements StatisticsDao {
    @Override
    public List<Map<String, String>> empStatistics(Integer empId) {
        List<Map<String,String>> list = new ArrayList<>();

        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT ");
        sb.append(" employeeId, ");
        sb.append(" date, ");
        sb.append(" DAYNAME(date) AS dayType, ");
        sb.append(" startTime, ");
        sb.append(" endTime, ");
        sb.append(" CASE ");
        sb.append(" WHEN startTime = '' or endTime='' THEN '上班或下班忘打卡' ");
        sb.append(" WHEN startTime > '09:00:00' THEN '迟到' ");
        sb.append(" WHEN endTime < '17:00:00' THEN '早退' ");
        sb.append(" ELSE '正常打卡' ");
        sb.append(" END AS attendance_status ");
        sb.append(" FROM attendance");
        sb.append(" WHERE ");
        sb.append(" employeeId =? ");
        sb.append(" ORDER BY date desc ");
        ResultSet resultSet = executeQuery(sb.toString(), empId);
        if(resultSet!=null){
            try {
                while (resultSet.next()){
                    Map<String,String> map = new HashMap<>();
                    map.put("employeeId", String.valueOf(resultSet.getInt("employeeId")));
                    map.put("date",resultSet.getString("date"));
                    map.put("dayType",resultSet.getString("dayType"));
                    map.put("startTime",resultSet.getString("startTime")==null?"":resultSet.getString("startTime"));
                    map.put("endTime",resultSet.getString("endTime")==null?"":resultSet.getString("endTime"));
                    map.put("attendance_status",resultSet.getString("attendance_status"));
                    list.add(map);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }


        return list;
    }
}
